<script setup>
import { ref, onMounted } from 'vue'
import PageContainer from '@/components/PageContainer.vue'
import ChannelSelect from '@/views/article/components/ChannelSelect.vue'
import { Delete, Edit } from '@element-plus/icons-vue'
import { artDeleteService, artListGet } from '@/api/article.js'
import { formatTime } from '../../utils/formate.js'
import ArticleEdit from '@/views/article/components/ArticleEdit.vue'
import { ElMessage, ElMessageBox } from 'element-plus'
const selectValue = ref({
  cate_id: '',
  state: '',
  pagenum: 1,
  pagesize: 2
})
// loading效果
const loading = ref(false)
const updateCate = (val) => {
  console.log(val)
}
const tableData = ref([]) //文章列表
const total = ref(0)
const handleEdit = (index, data) => {
  console.log(index, data)
  articleEdit.value.open(data)
}
const handleDelete = async (index, data) => {
  console.log(index, data)
  await ElMessageBox.confirm('是否删除该文章?', '提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning'
  })
  await artDeleteService(data.id)
  ElMessage.success('删除成功')
  await getArticles()
}

// 获取文章列表
const getArticles = async () => {
  loading.value = true
  let response = await artListGet(selectValue.value)
  tableData.value = response.data.data
  total.value = response.data.total
  loading.value = false
}
onMounted(() => {
  getArticles()
})
const handleSizeChange = (val) => {
  console.log(val)
  selectValue.value.pagesize = val
  selectValue.value.pagenum = 1
  getArticles()
}
const handleCurrentChange = (val) => {
  console.log(val)
  selectValue.value.pagenum = val
  getArticles()
}
const onSearch = () => {
  selectValue.value.pagenum = 1
  getArticles()
}
const onReset = () => {
  selectValue.value.pagenum = 1
  selectValue.value.pagesize = 2
  selectValue.value.cate_id = ''
  selectValue.value.state = ''
  getArticles()
}
// 编辑或新增文章抽屉部分
const articleEdit = ref()
</script>

<template>
  <PageContainer title="文章管理">
    <template #extra>
      <el-button type="primary" @click="articleEdit.open({})">新增文章</el-button>
    </template>
    <el-form label-width="auto" style="max-width: 100%" inline>
      <el-form-item label="文章分类:">
        <channelSelect
          v-model:modelValue="selectValue.cate_id"
          @update:modelValue="updateCate"
          width="240px"
        ></channelSelect>
      </el-form-item>
      <el-form-item label="发布状态:">
        <el-select v-model="selectValue.state" style="width: 240px">
          <el-option label="草稿" value="草稿" />
          <el-option label="已发布" value="已发布" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="onSearch">搜索</el-button>
        <el-button type="info" @click="onReset">重置</el-button>
      </el-form-item>
    </el-form>
    <el-table :data="tableData" style="width: 100%" v-loading="loading">
      <el-table-column prop="title" label="文章标题" width="380">
        <template #default="scope">
          <el-link type="primary" :underline="false">{{ scope.row.title }}</el-link>
        </template>
      </el-table-column>
      <el-table-column prop="cate_name" label="分类" />
      <el-table-column prop="pub_date" label="发表时间" width="300">
        <template #default="{ row }">
          {{ formatTime(row.pub_date) }}
        </template>
      </el-table-column>
      <el-table-column prop="state" label="状态" />
      <el-table-column label="操作">
        <template #default="scope">
          <el-button
            type="primary"
            :icon="Edit"
            circle
            @click="handleEdit(scope.$index, scope.row)"
          >
          </el-button>
          <el-button
            type="danger"
            :icon="Delete"
            circle
            @click="handleDelete(scope.$index, scope.row)"
          >
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      v-model:current-page="selectValue.pagenum"
      v-model:page-size="selectValue.pagesize"
      :page-sizes="[2, 3, 5, 7]"
      :background="true"
      layout="jumper,total, sizes, prev, pager, next"
      :total="total"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      style="margin-top: 20px; justify-content: flex-end"
    />
    <ArticleEdit ref="articleEdit" @updateArticle="getArticles"></ArticleEdit>
  </PageContainer>
</template>

<style scoped></style>
